package com.uenpay.im.ui.old.activity.company;

import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * @author SL
 * @created 2016-2-22 下午3:52:22
 * @copyRight http://www.qiyunxin.com
 * @function 获取部门下员工
 */
public class GetEntCustByDepartInvokeItem extends BaseHttpInvokeItem {

	public class GetEntCustByDepartInvokeItemResult extends HttpInvokeResult {
		public ArrayList<EmployeeEntity> arrayList = new ArrayList<EmployeeEntity>();
	}

	public GetEntCustByDepartInvokeItemResult getOutPut() {
		return (GetEntCustByDepartInvokeItemResult) GetResultObject();
	}

	/**
	 * 获取部门下员工
	 * 
	 * @param departid
	 *            部门id
	 * @param entid
	 *            企业id
	 */
	public GetEntCustByDepartInvokeItem(int departid, int entid) {
		String url = APIConfiguration.getBaseUrl()
				+ "Organization/Organization/getEntCustByDepart?departid="
				+ departid + "&entid=" + entid + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetEntCustByDepartInvokeItemResult result = new GetEntCustByDepartInvokeItemResult();
		try {
			JSONObject json = new JSONObject(responseBody);
			result.status = json.optInt("status");
			result.msg = json.optString("msg");

			JSONArray jsonarr = json.optJSONArray("data");
			if (jsonarr != null) {
				for (int i = 0, length = jsonarr.length(); i < length; i++) {
					result.arrayList.add(desEmployeeEntity(jsonarr
							.optJSONObject(i)));
				}
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return result;
		}

		return result;
	}

	private EmployeeEntity desEmployeeEntity(JSONObject json) {
		EmployeeEntity mEmployeeEntity = new EmployeeEntity();
		mEmployeeEntity.custid = json.optString("custid");
		mEmployeeEntity.nickname = json.optString("nickname");
		mEmployeeEntity.position = json.optString("position");
		mEmployeeEntity.mobile = json.optString("mobile");
		return mEmployeeEntity;
	}

}
